package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.BusinessProjectMapper;
import org.linlinjava.litemall.db.domain.BusinessProject;
import org.linlinjava.litemall.db.domain.BusinessProjectExample;
import org.linlinjava.litemall.db.domain.ProjectFileExample;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Author: Daisen.Z
 * @Date: 2021/6/3 18:45
 * @Version: 1.0
 * @Description:
 */
@Service
public class BusinessProjectService {

    @Resource
    private BusinessProjectMapper projectMapper;

    // 查询与自己相关的业务
    public List<BusinessProject> querySelectiveByPojectIds(String adminName, List<String> projectIds, String year, String name, String type,
                                                           Integer statusAudit, String dutyPeopleName, Integer page, Integer size, String sort, String order) {
        BusinessProjectExample example = new BusinessProjectExample();
        BusinessProjectExample.Criteria criteria = example.createCriteria();
        BusinessProjectExample.Criteria orCriteria = example.createCriteria();

        if (!StringUtils.isEmpty(year)) {
            criteria.andProjectExecuteYearEqualTo(year);
            orCriteria.andProjectExecuteYearEqualTo(year);
        }

        // 项目名称
        if (!StringUtils.isEmpty(name)) {
            criteria.andProjectNameLike("%" + name + "%");
            orCriteria.andProjectNameLike("%" + name + "%");
        }

        // 项目类型
        if (!StringUtils.isEmpty(type)) {
            criteria.andProjectTypeLike("%" + type + "%");
            orCriteria.andProjectTypeLike("%" + type + "%");
        }

        if (!StringUtils.isEmpty(statusAudit)) {
            criteria.andStatusAuditEqualTo(statusAudit);
            orCriteria.andStatusAuditEqualTo(statusAudit);
        }

        // 负责人
        if (!StringUtils.isEmpty(dutyPeopleName)) {
            criteria.andDutyPersonalNameLike("%" + dutyPeopleName + "%");
        }

        // 自己参与的项目
        if (!CollectionUtils.isEmpty(projectIds)){
            criteria.andProjectIdIn(projectIds);
        }

        // 或者录入人是自己也可以
        if (!StringUtils.isEmpty(adminName)){
            orCriteria.andAdminNameEqualTo(adminName);
            example.or(orCriteria);
        }

        //if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        //    example.setOrderByClause(sort + " " + order);
        //}


        PageHelper.startPage(page, size);
        return projectMapper.selectByExample(example);
    }

    public int deleteByProjectId(String projectId) {
        return projectMapper.deleteByPrimaryKey(projectId);
    }

    public BusinessProject selectByPrimaryKey(String projectId) {
        return projectMapper.selectByPrimaryKey(projectId);
    }

    public String selectNameByIdIn(String projectIds) {
        BusinessProjectExample example = new BusinessProjectExample();
        BusinessProjectExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(projectIds)){
            String[] ids = projectIds.split(",");
            criteria.andProjectIdIn(Arrays.asList(ids));
        }
        List<BusinessProject> projectList = projectMapper.selectByExampleSelective(example, BusinessProject.Column.projectName);
        String msg = "";
        if (!CollectionUtils.isEmpty(projectList)){
            for (BusinessProject businessProject : projectList) {
                msg+=businessProject.getProjectName() + ",";
            }
        }
        return msg;
    }

    public List<BusinessProject> querySelective(String year, String name, String type, Integer statusAudit, String dutyPeopleName,
                                                Integer page, Integer size, String sort, String order) {
        BusinessProjectExample example = new BusinessProjectExample();
        BusinessProjectExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(year)) {
            criteria.andProjectExecuteYearEqualTo(year);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andProjectNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(type)) {
            criteria.andProjectTypeLike("%" + type + "%");
        }
        if (!StringUtils.isEmpty(statusAudit)) {
            criteria.andStatusAuditEqualTo(statusAudit);
        }

        //if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        //    example.setOrderByClause(sort + " " + order);
        //}
        // 负责人
        if (!StringUtils.isEmpty(dutyPeopleName)) {
            criteria.andDutyPersonalNameLike("%" + dutyPeopleName + "%");
        }


        PageHelper.startPage(page, size);
        return projectMapper.selectByExample(example);
    }

    public int insert(BusinessProject businessProject) {
        return projectMapper.insert(businessProject);
    }

    public List<BusinessProject> selectByStatusAudit(Integer statusAudit) {
        BusinessProjectExample example = new BusinessProjectExample();
        BusinessProjectExample.Criteria criteria = example.createCriteria();
        criteria.andStatusAuditEqualTo(statusAudit);
        example.orderBy("project_name");
        return projectMapper.selectByExample(example);
    }


    public int update(BusinessProject businessProject) {
        return projectMapper.updateByPrimaryKey(businessProject);
    }


    public int updateByPrimaryKeySelective(BusinessProject businessProject) {
        return projectMapper.updateByPrimaryKeySelective(businessProject);
    }

    public BusinessProject selectById(String projectId) {
        return projectMapper.selectByPrimaryKey(projectId);
    }
}
